ViewPLSresults.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. function varargout = ViewPLSresults(varargin)
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % Visualization HUB for viewing PLS 4D BRIK files. %
  4. % Last modified: Feb. 26, 2014 %
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6. % Copyright (C) 2013-2014, Michael J. Cheung
  7. %
  8. % This file is a part of the MEG & PLS Pipeline (MEGPLS). For more
  9. % details, see the documentation included with the software package.
  10. %
  11. % MEGPLS is free software: you can redistribute it and/or modify it under
  12. % the terms of the GNU General Public License version 2 as published by
  13. % the Free Software Foundation. This program is distributed in the hope
  14. % that it will be useful, but WITHOUT ANY WARRANTY; without even the
  15. % implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. % See the GNU General Public License for more details.
  17. %
  18. % You should have received a copy of the GNU General Public License along
  19. % with this program. If not, you can download the license here:
  20. % <http://www.gnu.org/licenses/old-licenses/gpl-2.0>.
  21. % Last Modified by GUIDE v2.5 20-Sep-2014 05:49:56
  22. % Begin initialization code - DO NOT EDIT
  23. gui_Singleton = 1;
  24. gui_State = struct('gui_Name', mfilename, ...
  25. 'gui_Singleton', gui_Singleton, ...
  26. 'gui_OpeningFcn', @ViewPLSresults_OpeningFcn, ...
  27. 'gui_OutputFcn', @ViewPLSresults_OutputFcn, ...
  28. 'gui_LayoutFcn', [] , ...
  29. 'gui_Callback', []);
  30. if nargin && ischar(varargin{1})
  31. gui_State.gui_Callback = str2func(varargin{1});
  32. end
  33. if nargout
  34. [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  35. else
  36. gui_mainfcn(gui_State, varargin{:});
  37. end
  38. % End initialization code - DO NOT EDIT
  39. %--- Executes just before ViewPLSresults is made visible. ---%
  40. %------------------------------------------------------------%
  41. function ViewPLSresults_OpeningFcn(hObject, eventdata, handles, varargin)
  42. % This function has no output args, see OutputFcn.
  43. % hObject handle to figure
  44. % eventdata reserved - to be defined in a future version of MATLAB
  45. % handles structure with handles and user data (see GUIDATA)
  46. % varargin command line arguments to ViewPLSresults (see VARARGIN)
  47. % Choose default command line output for ViewPLSresults
  48. handles.output = hObject;
  49. % Make sure toolbox paths are added:
  50. [PipelineDir, ~, ~] = fileparts(which('ViewPLSresults.m'));
  51. addpath(genpath(PipelineDir));
  52. rmpath([PipelineDir,'/DEFAULT_SETTINGS']); % Make sure its calling from AnalysisID
  53. rmpath([PipelineDir,'/TEMPORARY_FIXES']); % Make sure its calling from FT toolbox
  54. CheckToolboxPaths(PipelineDir);
  55. % Initialize variables:
  56. handles.paths.OrigAnatFile = ...
  57. [PipelineDir,'/MEGPLS_TOOLBOX/Masks_Templates/template_ch2+tlrc.BRIK'];
  58. set(handles.TextboxAnatFile, 'String', handles.paths.OrigAnatFile);
  59. handles.gui.PLSimgList = [];
  60. handles.paths.PLSresultsDir = [];
  61. handles.paths.CurrentPLSimg = [];
  62. handles.gui.GrpAvgCorrFileList = [];
  63. handles.paths.CurrentGrpAvgCorrFiles = [];
  64. % Update handles structure
  65. guidata(hObject, handles);
  66. % UIWAIT makes ViewPLSresults wait for user response (see UIRESUME)
  67. % uiwait(handles.figure1);
  68. %--- Outputs from this function are returned to the command line. ---%
  69. %--------------------------------------------------------------------%
  70. function varargout = ViewPLSresults_OutputFcn(hObject, eventdata, handles)
  71. % varargout cell array for returning output args (see VARARGOUT);
  72. % hObject handle to figure
  73. % eventdata reserved - to be defined in a future version of MATLAB
  74. % handles structure with handles and user data (see GUIDATA)
  75. % Get default command line output from handles structure
  76. varargout{1} = handles.output;
  77. %====================================%
  78. % FUNCTIONS FOR ANATOMICAL UNDERLAY: %
  79. %====================================%
  80. %--- Textbox to display selected anatomical .BRIK file: ---%
  81. %----------------------------------------------------------%
  82. function TextboxAnatFile_Callback(hObject, eventdata, handles)
  83. EnteredText = get(handles.TextboxAnatFile, 'String');
  84. if ~isequal(EnteredText, handles.paths.OrigAnatFile)
  85. set(handles.TextboxAnatFile, 'String', handles.paths.OrigAnatFile);
  86. msgbox('Note: Use button to change anatomy file.');
  87. end
  88. %--- Executes on button press in ButtonAnatFile. ---%
  89. %---------------------------------------------------%
  90. function ButtonAnatFile_Callback(hObject, eventdata, handles)
  91. [AnatPath, ~, ~] = fileparts(handles.paths.OrigAnatFile);
  92. [BrikFile, BrikPath] = uigetfile([AnatPath,'/*+tlrc.BRIK'], ...
  93. 'Select anatomical underlay .BRIK file.', 'MultiSelect', 'off');
  94. if BrikFile == 0
  95. return; % If user cancels
  96. end
  97. % Set anatomy file:
  98. handles.paths.OrigAnatFile = [BrikPath,BrikFile];
  99. set(handles.TextboxAnatFile, 'String', handles.paths.OrigAnatFile)
  100. % Save handles:
  101. guidata(hObject, handles);
  102. %=================================================%
  103. % FUNCTIONS FOR PLS IMAGE & TIMESERIES SELECTION: %
  104. %=================================================%
  105. %--- Textbox to display selected PLS results directory: ---%
  106. %----------------------------------------------------------%
  107. function TextboxPLSresultsDir_Callback(hObject, eventdata, handles)
  108. EnteredText = get(handles.TextboxPLSresultsDir, 'String');
  109. if ~isequal(EnteredText, handles.paths.PLSresultsDir)
  110. set(handles.TextboxPLSresultsDir, 'String', handles.paths.PLSresultsDir);
  111. msgbox('Note: Use button to change PLS results directory.');
  112. end
  113. %--- Executes on selection change in ListboxPLSresultsFiles. ---%
  114. %---------------------------------------------------------------%
  115. function ListboxPLSresultsFiles_Callback(hObject, eventdata, handles)
  116. if isempty(handles.paths.PLSresultsDir) || isempty(handles.gui.PLSimgList)
  117. return;
  118. end
  119. handles = DetectSelectedFiles(handles);
  120. guidata(hObject, handles);
  121. %--- Executes on selection change in ListboxGrpAvgCorrFiles. ---%
  122. %---------------------------------------------------------------%
  123. function ListboxGrpAvgCorrFiles_Callback(hObject, eventdata, handles)
  124. if isempty(handles.paths.PLSresultsDir) || isempty(handles.gui.GrpAvgCorrFileList)
  125. return;
  126. end
  127. handles = DetectSelectedFiles(handles);
  128. guidata(hObject, handles);
  129. %--- Executes on button press in ButtonPLSresultsDir. ---%
  130. %--------------------------------------------------------%
  131. function ButtonPLSresultsDir_Callback(hObject, eventdata, handles)
  132. if ~isempty(handles.paths.PLSresultsDir)
  133. [OneUpDir, ~, ~] = fileparts(handles.paths.PLSresultsDir);
  134. else
  135. OneUpDir = [];
  136. end
  137. SelectedPath = uigetdir(OneUpDir, 'Select PLS results folder:');
  138. if SelectedPath == 0
  139. return; % If user cancels
  140. end
  141. % Get list of PLS image files & GrpAvg or Corr timeseries files:
  142. PLSimgFiles = dir([SelectedPath,'/BSRimg_*_LV*+tlrc.BRIK']);
  143. PLSimgFiles = [PLSimgFiles; dir([SelectedPath,'/SALimg_*_LV*+tlrc.BRIK'])];
  144. if isempty(PLSimgFiles)
  145. msgbox('Error: No PLS image files found inside directory.', 'Error:')
  146. return;
  147. end
  148. TimeseriesFiles = dir([SelectedPath,'/*TS_*+tlrc.BRIK']);
  149. if isempty(TimeseriesFiles)
  150. msgbox('Error: No GrpAvg / Corr timeseries files found inside directory.', 'Error:')
  151. return;
  152. end
  153. % Set current path and available files:
  154. handles.paths.PLSresultsDir = SelectedPath;
  155. set(handles.TextboxPLSresultsDir, 'String', handles.paths.PLSresultsDir);
  156. handles.gui.PLSimgList = {PLSimgFiles.name}';
  157. handles.gui.GrpAvgCorrFileList = {TimeseriesFiles.name}';
  158. handles = UpdatePLSimgTimeseries (handles);
  159. handles = DetectSelectedFiles (handles);
  160. % Save handles:
  161. guidata(hObject, handles);
  162. %--- Update PLSimg & timeseries settings: ---%
  163. %--------------------------------------------%
  164. function OutputHandles = UpdatePLSimgTimeseries(InputHandles)
  165. handles = InputHandles;
  166. % Update PLSimg listbox:
  167. set(handles.ListboxPLSresultsFiles, 'String', handles.gui.PLSimgList);
  168. CurrentIndex = get(handles.ListboxPLSresultsFiles, 'Value');
  169. MaxIndex = length(handles.gui.PLSimgList);
  170. if isempty(CurrentIndex) || CurrentIndex == 0 || CurrentIndex > MaxIndex
  171. set(handles.ListboxPLSresultsFiles, 'Value', MaxIndex);
  172. end
  173. % Update GrpAvg / Corr timeseries listbox:
  174. set(handles.ListboxGrpAvgCorrFiles, 'String', handles.gui.GrpAvgCorrFileList);
  175. CurrentIndex = get(handles.ListboxGrpAvgCorrFiles, 'Value');
  176. MaxIndex = length(handles.gui.GrpAvgCorrFileList);
  177. if max(CurrentIndex) > MaxIndex || min(CurrentIndex) < 1 || ...
  178. isempty(CurrentIndex) || (length(CurrentIndex) == 1 && CurrentIndex == 0)
  179. set(handles.ListboxGrpAvgCorrFiles, 'Value', MaxIndex);
  180. end
  181. % Set output handles:
  182. OutputHandles = handles;
  183. %--- Acquire currently selected PLSimg & timeseries files: ---%
  184. %-------------------------------------------------------------%
  185. function OutputHandles = DetectSelectedFiles(InputHandles)
  186. handles = InputHandles;
  187. handles.paths.CurrentPLSimg = [];
  188. handles.paths.CurrentGrpAvgCorrFiles = [];
  189. % Get selected PLSimg:
  190. SelectedIndex = get(handles.ListboxPLSresultsFiles, 'Value');
  191. SelectedFile = handles.gui.PLSimgList{SelectedIndex};
  192. handles.paths.CurrentPLSimg = strcat([handles.paths.PLSresultsDir,'/'], SelectedFile);
  193. % Get selected timeseries files:
  194. SelectedIndices = get(handles.ListboxGrpAvgCorrFiles, 'Value');
  195. SelectedFiles = handles.gui.GrpAvgCorrFileList(SelectedIndices);
  196. handles.paths.CurrentGrpAvgCorrFiles = ...
  197. strcat([handles.paths.PLSresultsDir,'/'], SelectedFiles);
  198. % Set output handles:
  199. OutputHandles = handles;
  200. %--- Executes on button press in ButtonTimeLegend. ---%
  201. %-----------------------------------------------------%
  202. function ButtonTimeLegend_Callback(hObject, eventdata, handles)
  203. if isempty(handles.paths.PLSresultsDir)
  204. msgbox('Error: PLS results folder not selected.', 'Error:')
  205. return;
  206. end
  207. if exist([handles.paths.PLSresultsDir,'/BSRimg_TimeLegend.txt'], 'file')
  208. open([handles.paths.PLSresultsDir,'/BSRimg_TimeLegend.txt']);
  209. else
  210. msgbox('Error: TimeLegend file missing for PLS results.', 'Error:');
  211. return;
  212. end
  213. %--- Executes on button press in ButtonShowLVPlot. ---%
  214. %-----------------------------------------------------%
  215. function ButtonShowLVPlot_Callback(hObject, eventdata, handles)
  216. if isempty(handles.paths.PLSresultsDir)
  217. msgbox('Error: PLS results folder not selected.', 'Error:')
  218. return;
  219. end
  220. % Acquire LV figures for selection:
  221. DetectedFigs = dir([handles.paths.PLSresultsDir,'/*_LV*.fig']);
  222. if isempty(DetectedFigs)
  223. msgbox('Error: No LV figures were detected inside directory.', 'Error:');
  224. return;
  225. end
  226. % Select LV figure to open:
  227. DetectedFigs = {DetectedFigs.name};
  228. SelectedIndex = listdlg('PromptString', 'Select LV figure to open:',...
  229. 'ListSize', [300, 300], 'SelectionMode', 'single', 'ListString', DetectedFigs);
  230. if isempty(SelectedIndex)
  231. return; % If user cancels
  232. end
  233. SelectedFig = DetectedFigs{SelectedIndex};
  234. open([handles.paths.PLSresultsDir,'/',SelectedFig]);
  235. %=========================%
  236. % FUNCTIONS FOR PLOTTING: %
  237. %=========================%
  238. %--- Executes on button press in ButtonView4DBSR. ---%
  239. %----------------------------------------------------%
  240. function ButtonView4DBSR_Callback(hObject, eventdata, handles)
  241. if isempty(handles.paths.OrigAnatFile)
  242. msgbox('Error: Anatomical underlay not selected.', 'Error:');
  243. return;
  244. end
  245. if isempty(handles.paths.PLSresultsDir)
  246. msgbox('Error: PLS results directory not selected.', 'Error:');
  247. return;
  248. end
  249. if isempty(handles.paths.CurrentPLSimg)
  250. msgbox('Error: PLS image file not selected.', 'Error:');
  251. return;
  252. end
  253. if isempty(handles.paths.CurrentGrpAvgCorrFiles)
  254. msgbox('Error: GrpAvg / Corr timeseries file(s) not selected.', 'Error:');
  255. return;
  256. end
  257. % Check if paths called in AFNI functions have spaces (AFNI cannot support spaces):
  258. CheckSpaces1 = strfind(handles.paths.OrigAnatFile, ' ');
  259. CheckSpaces2 = strfind(handles.paths.PLSresultsDir, ' ');
  260. if ~isempty(CheckSpaces1) || ~isempty(CheckSpaces2)
  261. msgbox('Error: AFNI fcns cannot read folder & file paths containing spaces.', 'Error:')
  262. return;
  263. end
  264. % Update selected files:
  265. handles = UpdatePLSimgTimeseries (handles);
  266. handles = DetectSelectedFiles (handles);
  267. % Check if file is 4D:
  268. CurrentDir = pwd;
  269. Opt.format = 'vector';
  270. cd(handles.paths.PLSresultsDir);
  271. [~, PLSimg, ~, ~] = BrikLoad(handles.paths.CurrentPLSimg, Opt);
  272. cd(CurrentDir);
  273. if numel(size(PLSimg)) ~= 4
  274. msgbox('Error: Selected file is not 4D file. Use "View in AFNI" instead.');
  275. return;
  276. end
  277. % Copy anat file to PLS results directory & resample:
  278. [~, OrigAnatName, ~] = fileparts(handles.paths.OrigAnatFile);
  279. CopiedAnatFile = [handles.paths.PLSresultsDir,'/',OrigAnatName,'.BRIK'];
  280. if exist(CopiedAnatFile, 'file')
  281. delete(CopiedAnatFile);
  282. delete([handles.paths.PLSresultsDir,'/',OrigAnatName,'.HEAD']);
  283. end
  284. system(['3dcopy ',handles.paths.OrigAnatFile,' ',CopiedAnatFile]);
  285. ResampledAnat = MEGpipeline_AfniResampleAnat...
  286. (CopiedAnatFile, handles.paths.CurrentPLSimg);
  287. if isempty(ResampledAnat)
  288. msgbox('Error: Failed to resampled anatomy file.', 'Error:');
  289. return;
  290. end
  291. % Run View4D_BSR:
  292. View4D_BSR(ResampledAnat, ...
  293. handles.paths.CurrentPLSimg, handles.paths.CurrentGrpAvgCorrFiles);
  294. %--- Executes on button press in ButtonViewAFNI. ---%
  295. %---------------------------------------------------%
  296. function ButtonViewAFNI_Callback(hObject, eventdata, handles)
  297. if isempty(handles.paths.OrigAnatFile)
  298. msgbox('Error: Anatomical underlay not selected.', 'Error:');
  299. return;
  300. end
  301. if isempty(handles.paths.PLSresultsDir)
  302. msgbox('Error: PLS results directory not selected.', 'Error:');
  303. return;
  304. end
  305. if isempty(handles.paths.CurrentPLSimg)
  306. msgbox('Error: PLS image file not selected.', 'Error:');
  307. return;
  308. end
  309. % Check if paths called in AFNI functions have spaces (AFNI cannot support spaces):
  310. CheckSpaces1 = strfind(handles.paths.OrigAnatFile, ' ');
  311. CheckSpaces2 = strfind(handles.paths.PLSresultsDir, ' ');
  312. if ~isempty(CheckSpaces1) || ~isempty(CheckSpaces2)
  313. msgbox('Error: AFNI fcns cannot read folder & file paths containing spaces.', 'Error:')
  314. return;
  315. end
  316. % Update selected files:
  317. handles = UpdatePLSimgTimeseries (handles);
  318. handles = DetectSelectedFiles (handles);
  319. % Copy anat file to PLS results directory & resample:
  320. [~, OrigAnatName, ~] = fileparts(handles.paths.OrigAnatFile);
  321. CopiedAnatFile = [handles.paths.PLSresultsDir,'/',OrigAnatName,'.BRIK'];
  322. if exist(CopiedAnatFile, 'file')
  323. delete(CopiedAnatFile);
  324. delete([handles.paths.PLSresultsDir,'/',OrigAnatName,'.HEAD']);
  325. end
  326. system(['3dcopy ',handles.paths.OrigAnatFile,' ',CopiedAnatFile]);
  327. ResampledAnat = MEGpipeline_AfniResampleAnat...
  328. (CopiedAnatFile, handles.paths.CurrentPLSimg);
  329. if isempty(ResampledAnat)
  330. msgbox('Error: Failed to resampled anatomy file.', 'Error:');
  331. return;
  332. end
  333. % Prepare file calls to AFNI:
  334. [~, AnatName, ~] = fileparts(CopiedAnatFile);
  335. AnatName(end-4:end) = []; % Remove +view label
  336. [~, PLSimgName, ~] = fileparts(handles.paths.CurrentPLSimg);
  337. PLSimgName(end-4:end) = [];
  338. % Set environmental variables:
  339. setenv('AFNI_SLAVE_FUNCTIME', 'YES'); % Locks overlay with time-index (Newer versions).
  340. setenv('AFNI_SLAVE_BUCKETS_TOO', 'YES'); % Locks overlay with time-index (Older versions).
  341. setenv('AFNI_SLAVE_THRTIME', 'YES'); % Locks threshold with time-index (Older versions).
  342. setenv('AFNI_SLAVE_THROLAY', 'OLay'); % Locks threshold with time-index (Newer versions).
  343. setenv('AFNI_LEFT_IS_LEFT', 'YES'); % Sets images to neurological view.
  344. setenv('AFNI_THRESH_AUTO', 'NO'); % Threshold slider does not change automatically.
  345. setenv('AFNI_THRESH_LOCK', 'VALUE');
  346. setenv('AFNI_FLOATSCAN', 'YES');
  347. if strcmp(getenv('AFNI_LEFT_IS_LEFT'), 'NO')
  348. msgbox('NOTE: "AFNI_LEFT_IS_LEFT" env. variable is now set to ''YES''.')
  349. end
  350. if strcmp(PLSimgName(1:7), 'SALimg_')
  351. CmdLine = ['afni -com ''' ...
  352. 'OPEN_WINDOW A.axialimage;' ...
  353. ' OPEN_WINDOW A.sagittalimage;' ...
  354. ' OPEN_WINDOW A.coronalimage;' ...
  355. ' SWITCH_UNDERLAY A.',AnatName,';' ... % Underlay .BRIK
  356. ' SWITCH_OVERLAY A.',PLSimgName,';' ... % Overlay .BRIK
  357. ' SET_SUBBRICKS A 1 1 1;' ... % Sets .BRIK index to 1
  358. ' SET_THRESHOLD A.0000 0;' ... % Threshold to 0 & power of 0
  359. ' SET_FUNC_AUTORANGE A.-;' ... % Turns off autorange
  360. ' SET_FUNC_RANGE A.0000.005;' ... % Sets range to 0.005
  361. ' SET_VIEW A.tlrc;' ... % Sets view to +tlrc
  362. ' REDISPLAY;'' &'];
  363. else
  364. CmdLine = ['afni -com ''' ...
  365. 'OPEN_WINDOW A.axialimage;' ...
  366. ' OPEN_WINDOW A.sagittalimage;' ...
  367. ' OPEN_WINDOW A.coronalimage;' ...
  368. ' SWITCH_UNDERLAY A.',AnatName,';' ... % Underlay .BRIK
  369. ' SWITCH_OVERLAY A.',PLSimgName,';' ... % Overlay .BRIK
  370. ' SET_SUBBRICKS A 1 1 1;' ... % Sets .BRIK index to 1
  371. ' SET_THRESHOLD A.2500 1;' ... % Threshold to 2.5 & power of 1
  372. ' SET_FUNC_AUTORANGE A.-;' ... % Turns off autorange
  373. ' SET_FUNC_RANGE A.0005;' ... % Sets range to 5
  374. ' SET_VIEW A.tlrc;' ... % Sets view to +tlrc
  375. ' REDISPLAY;'' &'];
  376. end
  377. CurrentDir = pwd;
  378. cd(handles.paths.PLSresultsDir);
  379. system(CmdLine);
  380. cd(CurrentDir);
  381. %--- Executes on button press in ButtonBrainScores. ---%
  382. %------------------------------------------------------%
  383. function ButtonBrainScores_Callback(hObject, eventdata, handles)
  384. if isempty(handles.paths.PLSresultsDir)
  385. msgbox('Error: PLS results directory not selected.', 'Error:');
  386. return;
  387. end
  388. % Get current BSRimg or SALimg selected:
  389. CurrentIndex = get(handles.ListboxPLSresultsFiles, 'Value');
  390. CurrentPLSres = handles.gui.PLSimgList{CurrentIndex};
  391. % Change prefix & suffix to get corresponding PLSres file:
  392. CurrentPLSres(1:6) = 'PLSres';
  393. CurrentPLSres((end-13):end) = [];
  394. CurrentPLSres((end+1):(end+4)) = '.mat';
  395. % Load PLS result .mat file:
  396. if ~exist([handles.paths.PLSresultsDir,'/',CurrentPLSres], 'file')
  397. msgbox('Error: PLS result .mat file could not be found for selected image.')
  398. return;
  399. end
  400. PLSresult = LoadFTmat...
  401. ([handles.paths.PLSresultsDir,'/',CurrentPLSres], 'ViewPLSresults');
  402. %** Old code...
  403. % PLSresultFiles = dir([handles.paths.PLSresultsDir,'/PLSres_*.mat']);
  404. %
  405. % if isempty(PLSresultFiles)
  406. % msgbox('Error: No PLS result .mat files found inside target directory.')
  407. % return;
  408. % end
  409. %
  410. % if length(PLSresultFiles) == 1
  411. % PLSresult = LoadFTmat...
  412. % ([handles.paths.PLSresultsDir,'/',PLSresultFiles(1).name], 'ViewPLSresults');
  413. % else
  414. % ErrMsg = {'Error:'; '';
  415. % 'Multiple PLS result .mat files found in the target directory.';
  416. % 'Each PLS run should be kept in its own respective folder.'; ''};
  417. % msgbox(ErrMsg, 'Error:');
  418. % return;
  419. % end
  420. % Load PLSmat .mat backup:
  421. PLSmatFile = [handles.paths.PLSresultsDir,'/Backup_PLSmat_Settings.mat'];
  422. if ~exist(PLSmatFile, 'file')
  423. msgbox('Error: Backup_PLSmat_Settings.mat file not found in target directory.')
  424. return;
  425. end
  426. PLSmat = LoadFTmat(PLSmatFile, 'ViewPLSresults');
  427. % Ask user which LV to plot:
  428. for LV = 1:size(PLSresult.v, 2)
  429. LVstring{LV} = ['LV',num2str(LV)];
  430. end
  431. LVindex = listdlg('PromptString', 'Select LV to plot Brain Scores for:',...
  432. 'ListSize', [300, 300], 'SelectionMode', 'single', 'ListString', LVstring);
  433. % Reshape brain score data for LV (for bar plot):
  434. NumCond = PLSresult.num_conditions;
  435. NumGrps = PLSresult.num_groups;
  436. BrainScore = PLSresult.usc(:, LVindex); % (Subj*Cond*Group)
  437. GrpStartIndex = 1;
  438. for g = 1:NumGrps
  439. NumGrpSubj = PLSresult.num_subj_lst(g); % # subj in grp
  440. NumGrpRows = NumGrpSubj * NumCond; % # total rows in grp
  441. GrpEndIndex = GrpStartIndex + NumGrpRows - 1;
  442. GrpBrainScore{g} = BrainScore(GrpStartIndex:GrpEndIndex); % {g}(Subj*Cond)
  443. GrpStartIndex = GrpEndIndex + 1; % Set start index for next grp
  444. GrpBrainScore{g} = reshape(GrpBrainScore{g}, NumGrpSubj, NumCond); % {g}(Subj, Cond)
  445. GrpBrainScore{g} = permute(GrpBrainScore{g}, [2 1]); % {g}(Cond, Subj)
  446. end
  447. % Plot brain score for each group: ** OR plot (Cond, Subj*Group)
  448. % This set will be visualized with line plots (line for each cond).
  449. for g = 1:NumGrps
  450. figure;
  451. plot(GrpBrainScore{g}');
  452. hold on;
  453. title(['LV',num2str(LVindex),' Brain Scores for: ',PLSmat.name.GroupID{g}]);
  454. clickableLegend(PLSmat.name.CondID)
  455. axis manual;
  456. set(gca, 'XLim', [1,length(PLSmat.name.SubjID{g})]);
  457. set(gca, 'XTick', [1:length(PLSmat.name.SubjID{g})]);
  458. set(gca, 'XTickLabel', PLSmat.name.SubjID{g});
  459. ylabel('Brain Scores:', 'Interpreter', 'none');
  460. xlabel('Subjects within Group:');
  461. end
  462. % Plot brain score for each group: ** OR plot (Cond, Subj*Group)
  463. % This set will be visualized with bar plots (bar for each subject).
  464. for g = 1:NumGrps
  465. figure;
  466. bar(GrpBrainScore{g});
  467. hold on;
  468. title(['LV',num2str(LVindex),' Brain Scores for: ',PLSmat.name.GroupID{g}]);
  469. clickableLegend(PLSmat.name.SubjID{g});
  470. axis manual
  471. set(gca, 'XTickLabel', PLSmat.name.CondID);
  472. ylabel('Brain Scores:', 'Interpreter', 'none');
  473. xlabel('Conditions:', 'Interpreter', 'none');
  474. hold off;
  475. end
  476. % Remove ErrorLog if no problems:
  477. if exist([pwd,'/ErrorLog_ViewPLSresults.txt'], 'file');
  478. LogCheck = dir('ErrorLog_ViewPLSresults.txt');
  479. if LogCheck.bytes == 0
  480. delete('ErrorLog_ViewPLSresults.txt');
  481. end
  482. end
  483. %--- Executes on button press in ButtonSingularValues. ---%
  484. %---------------------------------------------------------%
  485. function ButtonSingularValues_Callback(hObject, eventdata, handles)
  486. if isempty(handles.paths.PLSresultsDir)
  487. msgbox('Error: PLS results directory not selected.', 'Error:');
  488. return;
  489. end
  490. % Get current BSRimg or SALimg selected:
  491. CurrentIndex = get(handles.ListboxPLSresultsFiles, 'Value');
  492. CurrentPLSres = handles.gui.PLSimgList{CurrentIndex};
  493. % Change prefix & suffix to get corresponding PLSres file:
  494. CurrentPLSres(1:6) = 'PLSres';
  495. CurrentPLSres((end-13):end) = [];
  496. CurrentPLSres((end+1):(end+4)) = '.mat';
  497. % Load PLS result .mat file:
  498. if ~exist([handles.paths.PLSresultsDir,'/',CurrentPLSres], 'file')
  499. msgbox('Error: PLS result .mat file could not be found for selected image.')
  500. return;
  501. end
  502. PLSresult = LoadFTmat...
  503. ([handles.paths.PLSresultsDir,'/',CurrentPLSres], 'ViewPLSresults');
  504. %** Old code...
  505. % PLSresultFiles = dir([handles.paths.PLSresultsDir,'/PLSres_*.mat']);
  506. %
  507. % if isempty(PLSresultFiles)
  508. % msgbox('Error: No PLS result .mat files found inside target directory.')
  509. % return;
  510. % end
  511. %
  512. % if length(PLSresultFiles) == 1
  513. % PLSresult = LoadFTmat...
  514. % ([handles.paths.PLSresultsDir,'/',PLSresultFiles(1).name], 'ViewPLSresults');
  515. % else
  516. % ErrMsg = {'Error:'; '';
  517. % 'Multiple PLS result .mat files found in the target directory.';
  518. % 'Each PLS run should be kept in its own respective folder.'; ''};
  519. % msgbox(ErrMsg, 'Error:');
  520. % return;
  521. % end
  522. % Plot singular values for each LV in PLS run:
  523. figure;
  524. bar(PLSresult.s);
  525. hold on;
  526. % Get labels:
  527. %** title([handles.paths.PLSresultsDir,'/',PLSresultFiles(1).name], 'Interpreter', 'none');
  528. title([handles.paths.PLSresultsDir,'/',CurrentPLSres], 'Interpreter', 'none');
  529. for LV = 1:size(PLSresult.v, 2)
  530. LVstring{LV} = ['LV',num2str(LV)];
  531. end
  532. set(gca, 'XTickLabel', LVstring);
  533. ylabel('Singular Values');
  534. xlabel('Latent Variables:');
  535. hold off;
  536. % Remove ErrorLog if no problems:
  537. if exist([pwd,'/ErrorLog_ViewPLSresults.txt'], 'file');
  538. LogCheck = dir('ErrorLog_ViewPLSresults.txt');
  539. if LogCheck.bytes == 0
  540. delete('ErrorLog_ViewPLSresults.txt');
  541. end
  542. end
  543. %============================%
  544. % GUIDE CREATEFCN FUNCTIONS: %
  545. %============================%
  546. % --- Executes during object creation, after setting all properties.
  547. function TextboxAnatFile_CreateFcn(hObject, eventdata, handles)
  548. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  549. set(hObject,'BackgroundColor','white');
  550. end
  551. % --- Executes during object creation, after setting all properties.
  552. function TextboxPLSresultsDir_CreateFcn(hObject, eventdata, handles)
  553. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  554. set(hObject,'BackgroundColor','white');
  555. end
  556. % --- Executes during object creation, after setting all properties.
  557. function ListboxPLSresultsFiles_CreateFcn(hObject, eventdata, handles)
  558. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  559. set(hObject,'BackgroundColor','white');
  560. end
  561. % --- Executes during object creation, after setting all properties.
  562. function ListboxGrpAvgCorrFiles_CreateFcn(hObject, eventdata, handles)
  563. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  564. set(hObject,'BackgroundColor','white');
  565. end